package com.hdc.sell.controller;

import com.hdc.sell.Service.BuyerService;
import com.hdc.sell.Service.OrderService;
import com.hdc.sell.converter.OrderForm2OrderDTOConverter;
import com.hdc.sell.dto.OrderDTO;
import com.hdc.sell.enums.ResultEnum;
import com.hdc.sell.exception.SellException;
import com.hdc.sell.form.OrderForm;
import com.hdc.sell.utils.ResultVOUtil;
import com.hdc.sell.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import javax.validation.Valid;
import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭丽然
 * @program: sell
 * @date 2020/5/20 19:18
 * 功能：
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    //1创建订单api：/buyer/order/create
    @PostMapping("/create")
    public ResultVO<Map<Spring,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult) {
        //1判断表单验证结果
        if (bindingResult.hasErrors()) {
            log.error("[创建订单]参数不正确");
            throw new SellException(ResultEnum.PARAM_EEROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        //2转换orderForm为OrderDTO
        OrderDTO orderDTO= OrderForm2OrderDTOConverter.conver(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("[创建订单]购物车不能为空");
            throw new SellException(ResultEnum.CARTEMPTY);
        }
        //3判断订单详情/购物车列表是否为空
        OrderDTO createResult= orderService.create(orderDTO);
        //4调用orderservice的create方法创建订单
        Map<String,String> map = new HashMap<>();
        map.put("orderId",createResult.getOrderId());
        //5创建成功的订单id返还给前端
        return ResultVOUtil.success(map);

    }

    //2查询订单列表api：/buyer/order/list
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size",defaultValue = "2") Integer size) {
        //1判断openid是否为空
        if (StringUtils.isEmpty(openid)) {
            log.error("[查询订单列表]openid为空");
            throw new SellException(ResultEnum.PARAM_EEROR);
        }
        //2 定义分页条件
        PageRequest request=PageRequest.of(page,size);
        //3调用service对象方法完成查询
        Page<OrderDTO> orderDTOPage=orderService.findList(openid,request);
        //4返回结果
        return  ResultVOUtil.success(orderDTOPage.getContent());

    }

    //3查询订单详情api：/buyer/order/detail
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId ){
        OrderDTO orderDTO =buyerService.findOrderOne(openid, orderId);
        return ResultVOUtil.success(orderDTO);

    }

    //4取消订单api：/buyer/order/cancel
    @PostMapping("cancel")
    public ResultVO  cancel(@RequestParam("openid") String openid,
                            @RequestParam("orderId") String orderId) {
        buyerService.cancelOrder(openid, orderId);
        return ResultVOUtil.success();
    }

}
